package com.micro.ai.template.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.micro.ai.commons.domain.ApiResponse;
import com.micro.ai.template.entity.TemplateVersion;
import com.micro.ai.template.service.TemplateVersionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 模板版本Controller
 *
 * @author micro-ai
 * @since 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/api/template/versions")
@RequiredArgsConstructor
@Tag(name = "模板版本管理", description = "模板版本相关接口")
public class TemplateVersionController {

    private final TemplateVersionService templateVersionService;

    @PostMapping
    @PreAuthorize("hasAuthority('template:version:create')")
    @Operation(summary = "创建新版本", description = "为模板创建新版本")
    @ApiResponses({
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "创建成功", 
                    content = @Content(schema = @Schema(implementation = TemplateVersion.class))),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "参数错误"),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "403", description = "权限不足")
    })
    public ApiResponse<TemplateVersion> createVersion(
            @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "版本信息", required = true)
            @Valid @RequestBody TemplateVersion templateVersion) {
        log.info("创建版本请求, templateId: {}", templateVersion.getTemplateId());
        return ApiResponse.success(templateVersionService.createVersion(templateVersion));
    }

    @GetMapping("/template/{templateId}")
    @PreAuthorize("hasAuthority('template:version:query')")
    @Operation(summary = "查询模板版本列表", description = "分页查询指定模板的所有版本")
    @ApiResponses({
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "查询成功"),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "403", description = "权限不足")
    })
    public ApiResponse<IPage<TemplateVersion>> getVersionsByTemplateId(
            @Parameter(description = "模板ID") @PathVariable String templateId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer size) {
        
        Page<TemplateVersion> page = new Page<>(current, size);
        return ApiResponse.success(templateVersionService.getVersionsByTemplateId(templateId, page));
    }

    @GetMapping("/template/{templateId}/active")
    @PreAuthorize("hasAuthority('template:version:query')")
    @Operation(summary = "查询活跃版本", description = "获取模板的当前活跃版本")
    @ApiResponses({
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "查询成功"),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "未找到活跃版本")
    })
    public ApiResponse<TemplateVersion> getActiveVersion(
            @Parameter(description = "模板ID") @PathVariable String templateId) {
        return ApiResponse.success(templateVersionService.getActiveVersion(templateId));
    }

    @PutMapping("/{versionId}/activate")
    @PreAuthorize("hasAuthority('template:version:update')")
    @Operation(summary = "激活版本", description = "将指定版本设置为活跃版本")
    @ApiResponses({
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "激活成功"),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "版本不存在")
    })
    public ApiResponse<Void> activateVersion(
            @Parameter(description = "版本ID") @PathVariable String versionId) {
        log.info("激活版本请求, versionId: {}", versionId);
        templateVersionService.activateVersion(versionId);
        return ApiResponse.<Void>success(null);
    }

    @GetMapping("/template/{templateId}/stable")
    @PreAuthorize("hasAuthority('template:version:query')")
    @Operation(summary = "查询稳定版本列表", description = "获取模板的所有稳定版本")
    @ApiResponses({
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "查询成功")
    })
    public ApiResponse<List<TemplateVersion>> getStableVersions(
            @Parameter(description = "模板ID") @PathVariable String templateId) {
        return ApiResponse.success(templateVersionService.getStableVersions(templateId));
    }

    @GetMapping("/compare")
    @PreAuthorize("hasAuthority('template:version:query')")
    @Operation(summary = "比较版本", description = "比较两个版本的差异")
    @ApiResponses({
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "比较成功"),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "版本不存在")
    })
    public ApiResponse<Object> compareVersions(
            @Parameter(description = "版本1 ID") @RequestParam String versionId1,
            @Parameter(description = "版本2 ID") @RequestParam String versionId2) {
        return ApiResponse.success(templateVersionService.compareVersions(versionId1, versionId2));
    }

    @PostMapping("/template/{templateId}/rollback")
    @PreAuthorize("hasAuthority('template:version:update')")
    @Operation(summary = "回滚版本", description = "回滚到指定版本")
    @ApiResponses({
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "回滚成功"),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "版本不存在")
    })
    public ApiResponse<Void> rollbackToVersion(
            @Parameter(description = "模板ID") @PathVariable String templateId,
            @Parameter(description = "目标版本ID") @RequestParam String versionId) {
        log.info("回滚版本请求, templateId: {}, versionId: {}", templateId, versionId);
        templateVersionService.rollbackToVersion(templateId, versionId);
        return ApiResponse.<Void>success(null);
    }
}

